'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2011 Jens Frenkel
'''
import unittest
import os
from wrapper.dymola import Dymola

class Test(unittest.TestCase):

    def test_open(self):
  
        # new directory
        workdir = './testDymolaopen'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testDymola.mo', 'w' )
        f.write('model testDymolaopen\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testDymolaopen;\n')
        f.close()   
        
        DYMOLAHOME = "C:/Program Files/Dymola 7.4"
        dymolapath ='%s/bin/Dymola.exe'%(DYMOLAHOME)
        
        dymola = Dymola(dymolapath)
        
        res, dt = dymola.open('testDymolaopen',['testDymola.mo'])
                
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        os.rmdir(workdir) 

    def test_flatten(self):
  
        # new directory
        workdir = './testDymolaflatten'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testDymola.mo', 'w' )
        f.write('model testDymolaflatten\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testDymolaflatten;\n')
        f.close()   
        
        DYMOLAHOME = "C:/Program Files/Dymola 7.4"
        dymolapath ='%s/bin/Dymola.exe'%(DYMOLAHOME)
        
        dymola = Dymola(dymolapath)
        
        res, dt = dymola.flatten('testDymolaflatten',['testDymola.mo'])
                
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        os.rmdir(workdir)   

    def test_translate(self):
  
        # new directory
        workdir = './testDymolatranslate'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testDymola.mo', 'w' )
        f.write('model testDymolatranslate\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testDymolatranslate;\n')
        f.close()   
        
        DYMOLAHOME = "C:/Program Files/Dymola 7.4"
        dymolapath ='%s/bin/Dymola.exe'%(DYMOLAHOME)
        
        dymola = Dymola(dymolapath)
        
        res, dt = dymola.translate('testDymolatranslate',['testDymola.mo'])
        
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        os.rmdir(workdir)   

    def test_simulate(self):
  
        # new directory
        workdir = './testDymolasimulate'
        if not os.access(workdir, os.F_OK):
            os.mkdir(workdir)
        # enter
        oldpath = os.getcwd()
        os.chdir(workdir)     
        
        # generate Testmodel
        f = open( 'testDymola.mo', 'w' )
        f.write('model testDymolasimulate\n')
        f.write('  Real x;\n')
        f.write('equation\n')
        f.write('  der(x) = x;\n')
        f.write('end testDymolasimulate;\n')
        f.close()   
        
        DYMOLAHOME = "C:/Program Files/Dymola 7.4"
        dymolapath ='%s/bin/Dymola.exe'%(DYMOLAHOME)
        
        dymola = Dymola(dymolapath)
        
        res, dt = dymola.simulate('testDymolasimulate',['testDymola.mo'],0,0.1,1)
        
        self.assertTrue( res is not False )
        
        files = os.listdir('./')
        for file in files:
            os.remove(file)
        
        os.chdir(oldpath)   
        os.rmdir(workdir)   

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_flatten']
    unittest.main()